package net.xdclass.online_xdclass.controller;

import net.xdclass.online_xdclass.model.entity.VideoOrder;
import net.xdclass.online_xdclass.model.request.VideoOrderRequest;
import net.xdclass.online_xdclass.service.VideoOrderService;
import net.xdclass.online_xdclass.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *  * @RestController可以返回json数据 @Controller 是不会返回json数据的，
 *  * @RequestMapping 定义api @RequestMapping 作⽤：路由映射，⽤于类上做1级路径；⽤于某个⽅法上做⼦路径
 *  * /api/v1/pub/AA/BB 这个是不需要登录
 *  * /api/v1/pri/AA/BB 这个是需要登录
 */
@RestController
@RequestMapping("api/v1/pri/order")
public class VideoOrderController {

    @Autowired
    VideoOrderService videoOrderService;

    /**
     * 下单接口
     * @return
     */
    @PostMapping("save")
    public JsonData saveOrder(@RequestBody VideoOrderRequest videoOrderRequest, HttpServletRequest request){
        Integer userId = (Integer) request.getAttribute("user_id");

        int rows = videoOrderService.save(userId, videoOrderRequest.getVideoId());
        return rows == 0 ? JsonData.buildError("下单失败"): JsonData.buildSuccess();
    }

    /**
     * 订单列表
     * @param request
     * @return
     */
    @GetMapping("list")
    public JsonData listOrder(HttpServletRequest request){
        Integer userId = (Integer) request.getAttribute("user_id");

        List<VideoOrder> videoOrderList = videoOrderService.listOrderByUserId(userId);

        return JsonData.buildSuccess(videoOrderList);
    }
}
